// pages/history/history.js

import api from '../../utils/api.js';
import util from '../../utils/util.js';
import { checkAndFetchToken, refreshToken } from '../../utils/Logic.js';
import net from '../../utils/Request.js';

const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        historyData: [],
        currentDate: null,
        today: null,
        loadMoreOrRefresh: -1, //-1 停止  0下拉  1程序自动刷新,
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        this.setData({
            currentDate: util.getMonthFirstDay(new Date())
        });
        checkAndFetchToken(this.route, this.fetchData, { 'loadMoreOrRefresh': 1 });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

        if (this.data.loadMoreOrRefresh < 0) {
            wx.showNavigationBarLoading();
            this.fetchData({ 'loadMoreOrRefresh': 0 });
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    fetchData: function (status) {
        //加载中....
        if (this.data.isLoading) {
            return;
        }
        console.log(status);
        var loadMode = 1;
        if (status != null && status.loadMoreOrRefresh === null) {
            loadMode = status.loadMoreOrRefresh;
        }
        this.setData({
            isLoadMore: true,
            loadMoreOrRefresh: loadMode
        });
        
        var that = this;
        wx.showNavigationBarLoading();
        net.post(api.baseUrl + 'api/getHistoryOrder', {
            ...app.globalData.access_token,
            'startDate': that.data.currentDate,
            'page': 0,
            'pageCount': 40
        }).then(res => res.data).then(res => {
            console.log(res);
            if (res.status === 20000) {
                that.setData({
                    historyData: res.data.content,
                    totalPages: res.data.totalPages,
                    totalElements: res.data.totalElements
                });
            }
        }).catch(e => {
            if (e.statusCode === 401) {
                refreshToken(that.route);
            }
            console.error('history load data error ', e);
        }).then(() => {
            that.setData({
                loadMoreOrRefresh: -1,
                isLoadMore: false
            });
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        });


    },

    itemOrderClick: function (e) {
        console.log(e);
    },


    bindDateChange: function (e) {
        console.log(e.detail.value);
        this.setData({
            currentDate: e.detail.value,
            historyData: []
        });

        this.fetchData(app.globalData.access_token, { 'loadMoreOrRefresh': 2 });
    }
})